<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">

      <el-form-item label="供应商名称">
        <el-input v-model="queryParams.supplier" clearable></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>



    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增供应商</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="AttenceStaList" >
      <el-table-column label="供应商" align="center" prop="supplier" :show-overflow-tooltip="true" />
      <el-table-column label="供应商电话" align="center" prop="supplierPhone" :show-overflow-tooltip="true" />
      <el-table-column label="营业执照编号" align="center" prop="businessLicenseCode" :show-overflow-tooltip="true" />
      <el-table-column label="描述" align="center" prop="supplierDesc" :show-overflow-tooltip="true" />
      <el-table-column label="是否有效" align="center" prop="supplierStatic" :show-overflow-tooltip="true">
          <template slot-scope="scope">
              <el-tag v-if="scope.row.supplierStatus==1" type="success">有效</el-tag>
              <el-tag v-if="scope.row.supplierStatus==2" type="info">无效</el-tag>
          </template>
      </el-table-column>
      <el-table-column label="供应商商品" align="center" prop="supplierStatic" :show-overflow-tooltip="true">
          <template slot-scope="scope">
              <a @click="handleGoodsList(scope.row)">查看商品</a>
          </template>
      </el-table-column>
      <el-table-column label="操作" align="center" >
        <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="新增商品" placement="top" >
                <el-button 
                        size="mini"
                        circle type="primary"
                        icon="el-icon-shopping-bag-2"
                        @click="handleGoodsAdd(scope.row)"
                />
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="修改" placement="top" >
                <el-button 
                        size="mini"
                        circle type="warning"
                        icon="el-icon-edit"
                        @click="editSupplier(scope.row)"
                />
            </el-tooltip>  
            <el-tooltip class="item" effect="dark" content="删除" placement="top" >
                <el-button 
                        size="mini"
                        circle type="danger"
                        icon="el-icon-delete"
                        @click="deleteSupplier(scope.row)"
                />
            </el-tooltip>  
        </template>
      </el-table-column>
    </el-table>


    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <el-dialog title="编辑" :visible="outDialogVisible" width="35%">
        <el-form :model="formData" ref="formData" label-width="140px" :rules="rules">
            <el-form-item label="供应商名称" prop="supplier">
                <el-input v-model="formData.supplier"></el-input>
            </el-form-item>
            <el-form-item label="供应商别名" prop="supplierAlias">
                <el-input v-model="formData.supplierAlias"></el-input>
            </el-form-item>
            <el-form-item label="供应商姓名" prop="supplierPhone">
                <el-input v-model="formData.supplierName"></el-input>
            </el-form-item>
            <el-form-item label="供应商联系电话" prop="supplierPhone">
                <el-input v-model="formData.supplierPhone"></el-input>
            </el-form-item>
            <el-form-item label="营业执照注册号" >
                <el-input v-model="formData.businessLicenseCode"></el-input>
            </el-form-item>
            <el-form-item label="供应资质证书编号">
                <el-input v-model="formData.qualificationsCode"></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <el-input v-model="formData.supplierDesc"></el-input>
            </el-form-item>
            <el-form-item label="供应商状态">
                <el-radio v-model="formData.supplierStatus" label="1">有效</el-radio>
                <el-radio v-model="formData.supplierStatus" label="2">无效</el-radio>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button  type="primary" @click="submitSupplier">提交</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>

    <el-dialog title="新增商品" :visible="addGoodsDialogVisible" width="30%" :before-close="cancelGoods">
        <el-form :model="goodsFormData" ref="goodsFormData" label-width="120px" :rules="goodsRules">
            <el-form-item label="商品名称" prop="goodsName">
                <el-input v-model="goodsFormData.goodsName"></el-input>
            </el-form-item>
            <el-form-item label="商品类型" prop="goodsType">
                <el-select v-model="goodsFormData.goodsType">
                    <el-option label="生鲜商品" value="1"></el-option>
                    <el-option label="普通商品" value="2"></el-option>
                    <el-option label="其他商品" value="3"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="规格" prop="goodsSpec">
                <el-input v-model="goodsFormData.goodsSpec"></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <el-input v-model="goodsFormData.goodsDesc"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button  type="primary" @click="submitGoods">提交</el-button>
            <el-button @click="cancelGoods">取 消</el-button>
        </div>
    </el-dialog>

    <el-dialog title="供应商商品" :visible="goodsListDialogVisible" width="50%" :before-close="closeGoodsList">
        <el-table v-loading="loading" :data="goodsList" >
      <el-table-column label="商品名称" align="center" prop="goodsName" :show-overflow-tooltip="true" />
      <el-table-column label="商品规格" align="center" prop="goodsSpec" :show-overflow-tooltip="true" />
      <el-table-column label="商品类型" align="center" prop="goodsType" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.goodsType==1" type="success">生鲜商品</el-tag>
          <el-tag v-if="scope.row.goodsType==2">普通商品</el-tag>
          <el-tag v-if="scope.row.goodsType==3">其他商品</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否有效" align="center" prop="goodsStatus" :show-overflow-tooltip="true">
          <template slot-scope="scope">
              <el-tag v-if="scope.row.goodsStatus==1" type="success">有效</el-tag>
              <el-tag v-if="scope.row.goodsStatus==2" type="info">无效</el-tag>
          </template>
      </el-table-column>
      <el-table-column label="商品描述" align="center" prop="goodsDesc" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="新增商品" placement="top" >
                <el-button 
                        size="mini"
                        circle type="danger"
                        icon="el-icon-delete"
                        @click="deleteGoods(scope.row)"
                />
            </el-tooltip>
          </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="goodsTotal>0"
      :total="goodsTotal"
      :page.sync="queryGoodsParams.pageNum"
      :limit.sync="queryGoodsParams.pageSize"
      @pagination="getList"
    />
    </el-dialog>


  </div>
</template>



<script>
  import { getsupplierPageList,saveOrUpdateFoodSupplierInfo,deleteFoodSupplierById,deleteFoodSupplierGoodsById,
  saveOrUpdateFoodSupplierGoodsInfo,getsupplierGoodsPageList } from "@/api/canteen/supplier";
  export default {
    name: "stock",
    data(){
      return{
        serviceEnvPath:process.env.VUE_APP_BASE_API,
        // 遮罩层
        loading: true,
        goodsLoading:true,
        outDialogVisible:false,
        detailLoading:false,
        addGoodsDialogVisible:false,
        goodsListDialogVisible:false,
        // 总条数
        total: 0,
        AttenceStaList: [],
        goodsList:[],
        detailList:[],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10
        },
        queryGoodsParams:{
          pageNum: 1,
          pageSize: 10
        },
        formData:{
            supplier:undefined,
            supplierDesc:undefined,
            supplierStatus:"1",
        },
        goodsFormData:{
            supplierId:undefined
        },
        goodsUnitList:[],
        rules:{
            supplier:[{ required: true, message: '请输入供应商名称', trigger: 'blur' }],
            supplierPhone:[{ required: true, message: '请输入供应商电话', trigger: 'blur' }],
            supplierAlias:[{ required: true, message: '请输入供应商别名', trigger: 'blur' }]
        },
        goodsRules:{
            goodsName:[{ required: true, message: '请输入商品名称', trigger: 'blur' }],
            goodsSpec:[{ required: true, message: '请输入商品规格', trigger: 'blur' }],
            goodsType:[{ required: true, message: '请选择商品类型', trigger: 'blur' }],
        }
        
      }
    },
    components:{
      // bargraph
    },
    created() {
        this.getDicts("goods_unit").then(response => {
            this.goodsUnitList = response.data;
        });
        this.getList();
    },
    methods: {
        getUnit(unit){
            //获取物品单位
            for(let i=0;i<this.goodsUnitList.length;i++){
                if(unit==this.goodsUnitList[i].dictValue){
                    return this.goodsUnitList[i].dictLabel
                }
            }
        },
      /** 查询任务量列表 */
      getList() {
        this.loading = true;
        getsupplierPageList(this.queryParams).then(response => {
            this.AttenceStaList = response.data.list;
            this.total = response.data.total;
            this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.outDialogVisible =false
        this.formData={}
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      handleAdd(){
          this.outDialogVisible = true
          this.formData={}
          this.formData.supplierStatus="1"
      },
      submitSupplier(){
          this.$refs['formData'].validate((valid)=>{
              if(valid){
                  saveOrUpdateFoodSupplierInfo(this.formData).then(resp=>{
                    this.$message({  message: '提交成功', type: 'success'});
                    this.getList()
                    this.outDialogVisible=false
                  })
              }
          })
      },
      deleteSupplier(row){
          this.$confirm('供应商['+row.supplier+']将被删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(()=>{
            deleteFoodSupplierById(row).then(resp=>{
                this.$message({ type: 'success', message: '删除成功!' });
                this.getList()
            })
            
        })
      },
      editSupplier(row){
          this.outDialogVisible=true
          this.formData = row
      },
      handleGoodsAdd(row){
          this.addGoodsDialogVisible = true
          this.goodsFormData.supplierId = row.id
          this.goodsFormData.goodsStatus=1
          
      },
      submitGoods(){
          this.$refs['goodsFormData'].validate((valid)=>{
              if(valid){
                  this.goodsFormData.goodsStatus=1
                  saveOrUpdateFoodSupplierGoodsInfo(this.goodsFormData).then(resp=>{
                    this.$message({  message: '提交成功', type: 'success'});
                    this.addGoodsDialogVisible=false
                  })
              }
          })
      },
      cancelGoods(){
        this.addGoodsDialogVisible = false
        this.goodsFormData={}
      },
      handleGoodsList(row){
          this.goodsListDialogVisible=true
          this.queryGoodsParams.pageNum=1
          this.queryGoodsParams.supplierId = row.id
          this.getGoodsList()
      },
      getGoodsList(){
          const _this = this
          getsupplierGoodsPageList(this.queryGoodsParams).then(resp=>{
               _this.goodsList = resp.data.list;
               _this.goodsTotal = resp.data.total;
               _this.goodsLoading = false;
               console.log( _this.goodsList)
          })
      },
      closeGoodsList(){
          this.goodsListDialogVisible=false
      },
      deleteGoods(row){
          deleteFoodSupplierGoodsById(row).then(resp=>{
            this.$message({ type: 'success', message: '删除成功!' });
            this.getGoodsList()
          })
      }
    }
  }
</script>
